<?php

/**
 * ExtGenericBase class file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftwareB.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 *          EVALUATION VERSION - NOT FOR COMMERCIAL USE OR DISTRIBUTION.
 * @version 0.7.2
 */
abstract class ExtGenericBase extends ExtWidget
{
    private $script = null;

    public function parseNode(ExtHTMLParserNode $node, ExtASTWalker $astwalker)
    {
        parent::parseNode($node, $astwalker);
        $this->parseBodyScript($node);
    }

    public function renderGeneric($asobject=true)
    {
        /**
         * This function tries to inject the ID,xtype or ExtJS class type
         * within the configuration object
         */
        $script = $this->script;
        $script = preg_replace('!/\*.*?\*/!s', '', $script);
        $script = preg_replace('/\n\s*\n/', "\n", $script);
        $script = trim($script);
        if (!($script[0] == '{') || !($script[strlen($script) - 1] == '}'))
            throw new Exception(Ext4Yii::t('ext4yii', "Invalid ExtJS component configuration notation:\n {script} .\nUse: {config:value,config:value} nonation.", array('{script}' => $script)));
        $pos = strpos($this->script, '{');
        $head = trim(substr($this->script, 0, $pos + 1));
        $tail = trim(substr($this->script, $pos + 1));
        $this->deleteConfig('xclass');
        if ($this->parent instanceof ExtPanel && $this->parent->hasHtmlElements)
        {
            $this->deleteConfig('xtype');
            $conf = $this->implodeConfigs() . ($tail != '}' ? ',' : '');
            return sprintf('new %s(%s%s%s)', $this->getXclass(), $head, $conf, $tail);
        }
        else
        {
            $conf = $this->implodeConfigs() . ($tail != '}' ? ',' : '');
            return $head . $conf . $tail;
        }
    }


    private function parseBodyScript($node)
    {
        $body = $node->findPropertyByName('config');
        if (!is_null($body))
        {
            $this->script = $this->evaluateValue($this->removeScriptTags($body->innertext()));
            $body->innertext = '';
            $body->shiftUp();
        }
    }
}

?>
